<div class="alert alert-warning" ng-if="!!invalidBuildFilterResource">
    <p>Current build filter {{invalidBuildFilterResource.name}} is invalid and cannot be used any longer:</p>
    <p><i>{{invalidBuildFilterMessage}}</i></p>
    <p>It has been replaced by the default filter.</p>
    <p ng-if="!invalidBuildFilterResource.shared || branch._buildFilterShare">
        You should <a href ng-click="buildFilterRemove(invalidBuildFilterResource)">delete</a> this filter.
    </p>
</div>

<table class="ot-branch-build-view">
    <thead>
    <tr>
        <th colspan="3">
            &nbsp;
        </th>
        <th colspan="{{validationStampFilterCount(3)}}">
            <!-- Filter: edition - select all-->
            <button class="btn btn-default ot-validation-stamp-filter-edition"
                    ng-click="selectAllValidationStampFilter(validationStampFilter)"
                    title="Add all validation stamps to the {{validationStampFilter.name}} filter"
                    ng-show="validationStampFilter && validationStampFilterEdition">
                <span class="fa fa-check-square-o"></span>
                Select all for <b>{{validationStampFilter.name}}</b>
            </button>
            <!-- Filter: edition - select none -->
            <button class="btn btn-default ot-validation-stamp-filter-edition"
                    ng-click="selectNoneValidationStampFilter(validationStampFilter)"
                    title="Remove all validation stamps from the {{validationStampFilter.name}} filter"
                    ng-show="validationStampFilter && validationStampFilterEdition">
                <span class="fa fa-square-o"></span>
                Select none for <b>{{validationStampFilter.name}}</b>
            </button>
            <!-- Filter: button to stop edition -->
            <button class="btn btn-default ot-validation-stamp-filter-edition"
                    ng-click="stopDirectEditValidationStampFilter(validationStampFilter)"
                    title="Done with the edition of the {{validationStampFilter.name}} filter"
                    ng-show="validationStampFilter && validationStampFilterEdition">
                <span class="fa fa-eye-slash"></span>
                <b>{{validationStampFilter.name}}</b> done editing
            </button>
        </th>
    </tr>
    <!-- Validation stamps -->
    <tr>
        <th colspan="3" rowspan="2">
            <!-- Filter command -->
            <ng-include src="'app/view/fragment.branchBuildFilter.tpl.html'"></ng-include>
            <!-- Build diff extension commands -->
            <button ng-repeat="action in branchView.buildDiffActions" class="btn btn-default"
                    id="{{action.id}}-top"
                    ng-click="buildDiff(action)">
                {{action.name}}
            </button>
            <!-- Auto refresh -->
            <button class="ot-branch-build-view-auto-refresh"
                    ng-class="{
                    'ot-branch-build-view-auto-refresh-enabled': autoRefresh,
                    'ot-branch-build-view-auto-refresh-disabled': !autoRefresh
                    }"
                    title="{{autoRefresh ? 'Auto refresh is enabled' : 'Auto refresh is disabled'}}"
                    ng-click="toggleAutoRefresh()">
                <span class="fa fa-refresh"></span>
            </button>
            <!-- Validation stamp filters -->
            <div class="btn-group">
                <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"
                        title="Validation stamp filter"
                        ng-class="{
                            'btn-warning': validationStampFilter,
                            'btn-default': !validationStampFilter
                        }"
                >
                    <span class="fa fa-filter"></span>
                    <span class="caret"></span>
                </button>
                <ul class="dropdown-menu ot-validationstampfilter-list" role="menu">
                    <!-- List of filters -->
                    <li ng-repeat="vsf in branchValidationStampFilterResources.resources">
                        <a>
                            <!-- Selected? -->
                            <span class="fa fa-check"
                                  ng-show="validationStampFilter && validationStampFilter.id == vsf.id"></span>
                            <!-- Name -->
                            <span ng-click="selectBranchValidationStampFilter(vsf)">{{vsf.name}}</span>
                            <!-- Show filter type: branch, project or global -->
                            <b title="Global scope" ng-if="vsf.scope == 'GLOBAL'">[G]</b>
                            <b title="Project scope - {{branch.project.name}}" ng-if="vsf.scope == 'PROJECT'">[P]</b>
                            <!-- Sharing to project -->
                            <span class="ot-command fa fa-share"
                                  title="Share this filter at project level"
                                  ng-if="vsf._shareAtProject"
                                  ng-click="shareValidationStampFilterAtProject(vsf)"></span>
                            <!-- Sharing to global -->
                            <span class="ot-command fa fa-share-square"
                                  title="Share this filter at global level"
                                  ng-if="vsf._shareAtGlobal"
                                  ng-click="shareValidationStampFilterAtGlobal(vsf)"></span>
                            <!-- Edition mode: ON -->
                            <span class="ot-command fa fa-eye"
                                  title="Edit this filter using the current view"
                                  ng-if="vsf._update && !validationStampFilterEdition"
                                  ng-click="directEditValidationStampFilter(vsf)"></span>
                            <!-- Edition mode: OFF -->
                            <span class="ot-command fa fa-eye-slash"
                                  title="Stop editing this filter using the current view"
                                  ng-if="vsf._update && validationStampFilterEdition && validationStampFilter.id == vsf.id"
                                  ng-click="stopDirectEditValidationStampFilter(vsf)"></span>
                            <!-- Editing -->
                            <span class="ot-command-update ot-command"
                                  title="Edit this filter"
                                  ng-if="vsf._update"
                                  ng-click="editBranchValidationStampFilter(vsf)"></span>
                            <!-- Deleting -->
                            <span class="ot-command-delete ot-command"
                                  title="Delete this filter"
                                  ng-if="vsf._delete"
                                  ng-click="deleteBranchValidationStampFilter(vsf)"></span>
                        </a>
                    </li>
                    <li ng-if="branchValidationStampFilterResources.resources.length == 0" role="presentation"
                        class="dropdown-header">No validation stamp filter is available
                    </li>
                    <!-- Reset filters -->
                    <li class="divider" ng-if="validationStampFilter"></li>
                    <li ng-if="validationStampFilter">
                        <a ng-click="clearBranchValidationStampFilter()">
                            <span class="fa fa-eraser"></span>
                            Clear validation stamp filter
                        </a>
                    </li>
                    <!-- New filter -->
                    <li ng-if="branch._validationStampFilterCreate" class="divider"></li>
                    <li ng-if="branch._validationStampFilterCreate">
                        <a ng-click="newBranchValidationStampFilter()">
                            <span class="fa fa-plus"></span>
                            New filter...
                        </a>
                    </li>
                </ul>
            </div>
            <!-- Loading -->
            <button ng-show="loadingBuildView" class="btn btn-default" disabled="disabled">
                <span class="fa fa-spinner fa-spin"></span>
                Loading...
            </button>
        </th>
        <!-- All validation stamps -->
        <th ng-repeat="validationStamp in validationStamps | filter:validationStampFilterFn"
            class="ot-branch-build-view-validation"
            ng-class="{
                'ot-validation-stamp-filter-edition': validationStampFilter && validationStampFilter._update && validationStampFilterEdition
            }">
            <!-- VS filter edition ON -->
            <span ng-if="validationStampFilter && validationStampFilter._update && validationStampFilterEdition">
                <span class="ot-action"
                      ng-class="{
                        'ot-validation-stamp-filter-edition-selected': validationStampFilter.vsNames.indexOf(validationStamp.name) >= 0,
                        'ot-validation-stamp-filter-edition-unselected': validationStampFilter.vsNames.indexOf(validationStamp.name) < 0
                        }"
                      ng-click="toggleValidationStampFromFilter(validationStamp.name)"
                >
                    <ot-entity-image entity="validationStamp"></ot-entity-image>
                </span>
                <span
                        ng-if="validationStampFilter && validationStampFilter._update && validationStampFilterEdition && validationStampFilter.vsNames.indexOf(validationStamp.name) >= 0"
                        ng-click="removeValidationStampFromFilter(validationStampFilter, validationStamp.name)"
                        title="Removes the {{validationStamp.name}} validation stamp from the {{validationStampFilter.name}} filter"
                        class="fa fa-minus-circle text-danger ot-action"
                        style="font-size: 50%; vertical-align: bottom;"></span>
                <span
                        ng-if="validationStampFilter && validationStampFilter._update && validationStampFilterEdition && validationStampFilter.vsNames.indexOf(validationStamp.name) < 0"
                        ng-click="addValidationStampFromFilter(validationStampFilter, validationStamp.name)"
                        title="Adds the {{validationStamp.name}} validation stamp from the {{validationStampFilter.name}} filter"
                        class="fa fa-plus-circle text-success ot-action"
                        style="font-size: 50%; vertical-align: bottom;"></span>
            </span>
            <!-- VS filter edition OFF -->
            <span ng-if="!validationStampFilter || !validationStampFilter._update || !validationStampFilterEdition">
                <ot-entity-image entity="validationStamp"
                                 title="{{validationStamp.name}} - {{validationStamp.description}}"
                                 link="#/validationStamp/{{validationStamp.id}}"></ot-entity-image>
            </span>
        </th>
    </tr>
    <!-- Validation stamps decorations -->
    <tr>
        <!-- For each validation stamp -->
        <th ng-repeat="validationStamp in validationStamps | filter:validationStampFilterFn"
            class="ot-branch-build-view-validation">
            <ot-entity-embedded-decorations
                    decorations="validationStamp.decorations"></ot-entity-embedded-decorations>
        </th>
    </tr>
    </thead>
    <tbody>
    <tr ng-repeat="build in builds"
        ng-click="onBuildRowSelected($event, build)"
        class="ot-disable-select"
    >
        <td>
            <span ng-if="selectedBuild.from != selectedBuild.to && selectedBuild.from == build.id" class="fa fa-chevron-down"></span>
            <span ng-if="selectedBuild.from != selectedBuild.to && selectedBuild.to == build.id" class="fa fa-chevron-up"></span>
            <span ng-if="selectedBuild.from == selectedBuild.to && selectedBuild.to == build.id" class="fa fa-chevron-right"></span>
            <span ng-if="selectedBuild.to != build.id && selectedBuild.from != build.id">&nbsp;</span>
        </td>
        <td>
            <!-- Build name and link -->
            <a href="#/build/{{build.id}}" class="ot-branch-build-view-build-name">
                {{build.name}}
            </a>
            <!-- Build decorations -->
            <span class="ot-decoration-list">
                <ot-entity-embedded-decorations decorations="build.decorations"></ot-entity-embedded-decorations>
            </span>
            <!-- Signature, without the creator in the overview -->
            <div class="ot-branch-build-view-build-signature">
                {{build.creation.time | date:'mediumDate'}}
                {{build.creation.time | date:'shortTime'}}
                <!-- Run info - run time if available -->
                <span ng-if="build.runInfo.runTime" title="{{build.runInfo.runTime}} seconds">
                    &nbsp;-&nbsp;<ot-duration duration="build.runInfo.runTime" detail="false"></ot-duration>
                </span>
            </div>
        </td>
        <!-- Promotion runs -->
        <td>
            <span ng-repeat="promotionRun in build.promotionRuns">
                <span class="ot-promotion-run">
                    <span class="ot-action"
                          ng-click="displayPromotionRuns(build, promotionRun)">
                        <ot-entity-image
                                title="{{promotionRun.promotionLevel.name}} - {{promotionRun.creation.time | date:'shortDate'}}"
                                entity="promotionRun.promotionLevel"></ot-entity-image>
                    </span>
                </span>
            </span>
        </td>
        <!-- Validation runs -->
        <td ng-repeat="validation in build.validations | filter:validationStampRunViewFilter"
            class="ot-branch-build-view-validation">
            <!-- Not run and validation enabled -->
            <span id="validation-{{build.id}}-{{validation.validationStamp.id}}-validate" ng-if="validation.validationRuns.length == 0 && build.links._validate" class="ot-command"
                  ng-click="createValidationRun(build, validation.validationStamp)">
                <ot-validation-run-status-none></ot-validation-run-status-none>
            </span>
            <!-- Not run and validation not enabled -->
            <span id="validation-{{build.id}}-{{validation.validationStamp.id}}-none" ng-if="validation.validationRuns.length == 0 && !build.links._validate">
                <ot-validation-run-status-none></ot-validation-run-status-none>
            </span>
            <!-- Last status -->
            <span id="validation-{{build.id}}-{{validation.validationStamp.id}}-list" ng-if="validation.validationRuns.length > 0">
                <span class="ot-action" ng-click="displayValidationRuns(build, validation.validationStamp)">
                    <ot-validation-run-status status="validation.validationRuns[0].validationRunStatuses[0]"></ot-validation-run-status>
                </span>
            </span>
        </td>
    </tr>
    </tbody>
    <tfoot>
    <tr>
        <td colspan="{{validationStampFilterCount(3)}}">
            <!-- Filter command -->
            <ng-include src="'app/view/fragment.branchBuildFilter.tpl.html'"></ng-include>
            <!-- Build diff extension commands -->
            <button ng-repeat="action in branchView.buildDiffActions" class="btn btn-default"
                    id="{{action.id}}"
                    ng-click="buildDiff(action)">
                {{action.name}}
            </button>
            <!-- Auto refresh -->
            <button class="ot-branch-build-view-auto-refresh"
                    ng-class="{
                    'ot-branch-build-view-auto-refresh-enabled': autoRefresh,
                    'ot-branch-build-view-auto-refresh-disabled': !autoRefresh
                    }"
                    title="{{autoRefresh ? 'Auto refresh is enabled' : 'Auto refresh is disabled'}}"
                    ng-click="toggleAutoRefresh()">
                <span class="fa fa-refresh"></span>
            </button>
        </td>
    </tr>
    </tfoot>
</table>